package com.ruoyi.health.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.health.mapper.RegionMapper;
import com.ruoyi.health.domain.Region;
import com.ruoyi.health.service.IRegionService;

/**
 * 区域Service业务层处理
 * 
 * @author dalian
 * @date 2023-08-06
 */
@Service
public class RegionServiceImpl implements IRegionService 
{
    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询区域
     * 
     * @param id 区域主键
     * @return 区域
     */
    @Override
    public Region selectRegionById(Long id)
    {
        return regionMapper.selectRegionById(id);
    }

    /**
     * 查询区域列表
     * 
     * @param region 区域
     * @return 区域
     */
    @Override
    public List<Region> selectRegionList(Region region)
    {
        return regionMapper.selectRegionList(region);
    }

    /**
     * 新增区域
     * 
     * @param region 区域
     * @return 结果
     */
    @Override
    public int insertRegion(Region region)
    {
        return regionMapper.insertRegion(region);
    }

    /**
     * 修改区域
     * 
     * @param region 区域
     * @return 结果
     */
    @Override
    public int updateRegion(Region region)
    {
        return regionMapper.updateRegion(region);
    }


    /**
     * 删除区域信息
     * 
     * @param id 区域主键
     * @return 结果
     */
    @Override
    public int deleteRegionById(Long id) {
        //先查询该区域下面，是否有子区域，返回值>0表示有至少一个子区域，那么就提示不能删除
        long childrenRegionCount = regionMapper.selectCountChildrenRegion(id);
        if(childrenRegionCount>0){
            throw new ServiceException("删除失败！原因：该区域下存在子区域！");
        }
        return regionMapper.deleteRegionById(id);
    }

    /**
     * 查询区域树
     * 不支持条件查询
     * @return
     */
    @Override
    public List<Region> selectRegionTree() {
        List<Region> allRegions = regionMapper.selectAllRegions();
        List<Region> treeRoot = new ArrayList<>();
        //给树根添加一级节点
        for (Region region : allRegions) {
            if(region.getParentId() == 0){
                treeRoot.add(region);
            }
        }
        //给一级节点添加二级节点
        for (Region lv1Node : treeRoot) {
            List<Region> childrenList = getChildren(lv1Node.getId(), allRegions );
            lv1Node.setChildren(childrenList);
        }

        return treeRoot;
    }

    //【递归】生成树的孩子节点
    private static List<Region> getChildren(Long id, List<Region> allNodes) {
        List<Region> childrenList = new ArrayList<>();

        for (Region oneNode : allNodes) {
            if(oneNode.getParentId() == id){
                childrenList.add(oneNode);
            }
        }
        //递归，形成深层的树结构
        for (Region nextLevelNode : childrenList) {

            nextLevelNode.setChildren(getChildren(nextLevelNode.getId(),allNodes));
        }
        // 停止递归的条件
        if(childrenList.size()==0){
            return new ArrayList<Region>();   //最后一级是空列表
            // return null;                     //最后一级是null
        }
        return childrenList;
    }






}
